package uit.bus;

import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;

import com.google.android.maps.GeoPoint;

public class ThietLap extends Activity implements OnCheckedChangeListener {
	public Spinner spnn;
	public Spinner spab;
	public Spinner splt;
	public CheckBox cbBatDau;
	public CheckBox cbZoom;
	public String[] kcnn = { "300m", "500m", "700m" };
	public String[] ambao = { "Âm báo 1", "Âm báo 2", "Âm báo 3", "Âm báo 4" };
	public String[] lotrinh = { "Lượt đi", "Lượt về", "Cả đi và về" };
	public ArrayAdapter<String> adapternn;
	public ArrayAdapter<String> adapterab;
	public ArrayAdapter<String> adapterlt;
	public int latCenter;
	public int longCenter;
	public int zoom;
	public int ambaoCurrent;
	public int kcnnCurrent;
	public int ltCurrent;
	public SharedPrefren share;
	public SharedPreferences preFerences;
	MediaPlayer song1, song2, song3, song4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thietlap);
		spnn = (Spinner) findViewById(R.id.spnn);
		spab = (Spinner) findViewById(R.id.spab);
		splt = (Spinner) findViewById(R.id.splt);

		cbBatDau = (CheckBox) findViewById(R.id.cbBatDau);
		cbZoom = (CheckBox) findViewById(R.id.cbZoom);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			latCenter = extras.getInt("latCenter", 0);
			longCenter = extras.getInt("longCenter", 0);
			zoom = extras.getInt("zoom", 0);
		}

		preFerences = HCMBusActivity.getSharePred();
		share = new SharedPrefren(preFerences);
		cbBatDau.setChecked(share.getStateBD());
		cbZoom.setChecked(share.getStateZoom());

		song1 = MediaPlayer.create(this, R.raw.ringring1);
		song2 = MediaPlayer.create(this, R.raw.ringring2);
		song3 = MediaPlayer.create(this, R.raw.ringring3);
		song4 = MediaPlayer.create(this, R.raw.ringring4);

		adapternn = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, kcnn);
		adapterab = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, ambao);
		adapterlt = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, lotrinh);

		ambaoCurrent = share.getAmBao();
		kcnnCurrent = share.getKCNN();
		ltCurrent = share.getLoTrinh();

		spnn.setAdapter(adapternn);
		spnn.setSelection(kcnnCurrent);
		spnn.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				int index = arg0.getSelectedItemPosition();
				if (kcnnCurrent != index) {
					share.setKCNN(index);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spab.setAdapter(adapterab);
		spab.setSelection(ambaoCurrent);
		spab.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				int index = arg0.getSelectedItemPosition();

				if (ambaoCurrent != index) {
					share.setAmBao(index);
					if (index == 0) {
						song1.start();
					}
					if (index == 1) {
						song2.start();
					}
					if (index == 2) {
						song3.start();
					}
					if (index == 3) {
						song4.start();
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		splt.setAdapter(adapterlt);
		splt.setSelection(ltCurrent);
		splt.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				int index = arg0.getSelectedItemPosition();
				if (ltCurrent != index) {
					share.setLoTrinh(index);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});

		cbBatDau.setOnCheckedChangeListener(this);
		cbZoom.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch (buttonView.getId()) {
		case R.id.cbBatDau:
			GeoPoint point = new GeoPoint(latCenter, longCenter);
			share.setStartPlace(point, isChecked);
			share.setItemBD(isChecked);
			break;
		case R.id.cbZoom:
			share.setZoomLevel(zoom, isChecked);
			share.setItemZoom(isChecked);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		// return super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			song1.stop();
			song2.stop();
			song3.stop();
			song4.stop();
			finish();
		}
		return true;
	}
}
